package com.lancoo.znbkxx.teacher.ui.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.ComponentActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ClassAnswertraitInfor
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.aibk_adapter_item_answer_trait.view.*


class AibkAnswerTraitAdapter(
        val activity: ComponentActivity?,
        val picketPoint: Int,
        val classAnswertraitInfor: ClassAnswertraitInfor
): KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_answer_trait, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        val typeRightRate = classAnswertraitInfor.TypeList[position].TypeRightRate
        var genreName = classAnswertraitInfor.TypeList[position].GenreName
        if(genreName.isEmpty()){
            genreName = classAnswertraitInfor.TypeList[position].TypeName
        }
        if(typeRightRate<picketPoint){
            holder.convertView.IvTypeNameAbility.setImageResource(R.drawable.aibk_chart_background2_radius_orange_5)
        }else{
            holder.convertView.IvTypeNameAbility.setImageResource(R.drawable.aibk_chart_background2_radius_green_5)
        }
        showIvData(holder.convertView.IvTypeNameAbility, typeRightRate, holder.convertView.TvTypeName, genreName)
        holder.convertView.RlAnswerTraitView.setOnClickListener {
            val location = IntArray(2)
            holder.convertView.RlAnswerTraitView.getLocationOnScreen(location)
            val xX = location[0]
            val yY = location[1]
            Log.e("convertView","====XX=x坐标======="+xX)
            Log.e("convertView","====YY=y坐标======="+yY)
            clickListener!!.onClick(genreName, LancooMethod.killlingDouble(typeRightRate.toDouble())!!, xX,yY)
        }

    }

    override fun getItemCount(): Int {
        return classAnswertraitInfor.TypeList.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    private fun showIvData(Iv: ImageView, data: Float,
                           Tv: TextView, typeName: String){

        val para1 = Iv.layoutParams
        para1.height = DensityUtils.dp2px(activity, data * 2f)
        Iv.layoutParams = para1

        Tv.text = typeName
    }

    interface OnClickListener {
        fun onClick(genreName: String, typeRightRate: String,x: Int, y: Int)
    }

    private var clickListener: OnClickListener? = null

    fun setClickListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

}